using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FallenValkyrie
{
    class Level
    {
        #region Variables
        private Rectangle[] player_rectangle;
        private Rectangle[] items_rectangle;
        private Texture2D[] scrolling_items_texture;
        private Texture2D[] repeated_items_texture;
        private Vector2[] items_position;
        private bool[] item_IsScrollable;
        private int[] scaleWidth;
        private int[] scaleHeight;
        private int counts;
        private static int max = 20;
        private int player_texture_width;
        private int screenWidth;
        private int screenHeight;
        private float scrolling_speed_obj;
        private float scrolling_mul_bg;
        private bool consumed = false;
        private bool[] canConsume;
        #endregion
        EnemyGenerator generator;

        public Level(int playerWidth, int screen_width, int screen_height, float scrollSpeed_objects, float scrollMultiplier_bg) {
            scrolling_items_texture = new Texture2D[max];
            repeated_items_texture  = new Texture2D[max];
            canConsume              = new bool[max];
            player_rectangle        = new Rectangle[max];
            items_rectangle         = new Rectangle[max];
            items_position          = new Vector2[max];
            item_IsScrollable       = new bool[max];
            scaleWidth              = new int[max];
            scaleHeight             = new int[max];
            player_texture_width    = playerWidth;
            scrolling_speed_obj     = scrollSpeed_objects;
            scrolling_mul_bg        = scrollMultiplier_bg;
            screenWidth             = screen_width;
            screenHeight            = screen_height;
        }

        

        /// <summary>
        /// NOTE : First and second items to add is the background that you want to scroll.
        /// MAXIMUM OBJECTS : 20
        /// Texture2D - bg_objects : Objects' textures.
        /// Vector2 scrollItemPos : Objects' positions.
        /// int scale_width : Objects' Width.
        /// int scale_height : Objects' Height.
        /// bool scrollable : Should the object scroll along with others.
        /// </summary>
        /// <param name="bg_objects"></param>
        /// <param name="objectsPos"></param>
        /// <param name="scale_width"></param>
        /// <param name="scale_height"></param>
        /// <param name="scrollable"></param>
        public void addItem (Texture2D bg_objects , Vector2 objectsPos, int scale_width, int scale_height, bool scrollable) {
                scrolling_items_texture[counts] = bg_objects;
                items_position[counts]          = objectsPos;
                scaleWidth[counts]              = scale_width;
                scaleHeight[counts]             = scale_height;
                item_IsScrollable[counts++]     = scrollable;
        }

        public void addItem(Texture2D bg_objects, Vector2 objectsPos, int scale_width, int scale_height, bool scrollable, bool consumable) {
                canConsume[counts]              = consumable;
                scrolling_items_texture[counts] = bg_objects;
                items_position[counts]          = objectsPos;
                scaleWidth[counts]              = scale_width;
                scaleHeight[counts]             = scale_height;
                item_IsScrollable[counts++]     = scrollable;
        }

        /// <summary>
        /// In Construction! DO NOT USE
        /// </summary>
        /// <param name="repeated_objects"></param>
        /// <param name="bg_objects"></param>
        /// <param name="objectsPos"></param>
        /// <param name="scale_width"></param>
        /// <param name="scale_height"></param>
        /// <param name="scrollable"></param>
        public void addItem(Texture2D repeated_objects, Texture2D bg_objects, Vector2 objectsPos, int scale_width, int scale_height, bool scrollable) {
                scrolling_items_texture[counts] = bg_objects;
                repeated_items_texture[counts]  = repeated_objects;
                items_position[counts]          = objectsPos;
                scaleWidth[counts]              = scale_width;
                scaleHeight[counts]             = scale_height;
                item_IsScrollable[counts++]     = scrollable;
        }

        public void UpdateLevel(Vector2 player_position, Vector2 player_direction, int scrollingOffset)
        {
            for (int i = 0; i < counts; i++) {
                if (item_IsScrollable[i] ) {
                    //If Right
                    if (player_position.X >= screenWidth - 780
                        && player_direction.X > 1) 
                    {
                        items_position[0].X += scrolling_speed_obj * scrolling_mul_bg;
                        items_position[1].X += scrolling_speed_obj * scrolling_mul_bg;
                        items_position[i].X += scrolling_speed_obj;
                    }
                    //If Left 
                    
                /*    else if (player_position.X <= scrollingOffset
                        && player_direction.X < -1)
                    {
                        items_position[0].X -= scrolling_speed_obj * scrolling_mul_bg;
                        items_position[1].X -= scrolling_speed_obj * scrolling_mul_bg;
                        items_position[i].X -= scrolling_speed_obj;
                    }*/
                    //Went off screen
               //     if (items_position[0].X < 0)
                  //      items_position[0].X += scrolling_items_texture[0].Width;
                    if (items_position[0].X > scrolling_items_texture[0].Width)
                        items_position[0].X -= scrolling_items_texture[0].Width;

            //        if (items_position[1].X < 0)
               //         items_position[1].X += scrolling_items_texture[1].Width;
                    if (items_position[1].X > scrolling_items_texture[1].Width)
                        items_position[1].X -= scrolling_items_texture[1].Width; 
                }
            } 
        }

        public void UpdateLevel(Rectangle playerRect, Vector2 player_position, Vector2 player_direction, int scrollingOffset) {
            for (int i = 2; i < counts; i++) {
                if (item_IsScrollable[i]) {
                    //If Right
                    if (player_position.X >= screenWidth - 780
                        && player_direction.X > 1)
                    {
                        items_position[0].X += scrolling_speed_obj * scrolling_mul_bg;
                        items_position[1].X += scrolling_speed_obj * scrolling_mul_bg;
                        items_position[i].X += scrolling_speed_obj;
                       
                    }
                    //If Left
                        /*
                    else if (player_position.X <= scrollingOffset
                        && player_direction.X < -1)
                    {
                        items_position[0].X -= scrolling_speed_obj * scrolling_mul_bg;
                        items_position[1].X -= scrolling_speed_obj * scrolling_mul_bg;
                        items_position[i].X -= scrolling_speed_obj;
                    }
                    //Went off screen
                    if (items_position[0].X < 0)
                        items_position[0].X += scrolling_items_texture[0].Width;*/
                    if (items_position[0].X > scrolling_items_texture[0].Width)
                        items_position[0].X -= scrolling_items_texture[0].Width;

                  /*  if (items_position[1].X < 0)
                        items_position[1].X += scrolling_items_texture[1].Width;*/
                    if (items_position[1].X > scrolling_items_texture[1].Width)
                        items_position[1].X -= scrolling_items_texture[1].Width;
                }
                InteractedWithGameObjects(i, playerRect);
            }
        }

        public void DrawLevel(SpriteBatch spriteBatch) {
            spriteBatch.Begin();

            spriteBatch.Draw(scrolling_items_texture[0], new Rectangle((int)(-1 * items_position[0].X), 0,
               scaleWidth[0], scaleHeight[0]), Color.White);

            if (items_position[0].X > scrolling_items_texture[0].Width - screenWidth)
                spriteBatch.Draw(scrolling_items_texture[0], new Rectangle((int)(-1 * items_position[0].X) + scrolling_items_texture[0].Width, 0,
                    scaleWidth[0], scaleHeight[0]), Color.White);

            spriteBatch.Draw(scrolling_items_texture[1], new Rectangle((int)(-1 * items_position[1].X), 0,
                scaleWidth[1], scaleHeight[1]), Color.White);

            if (items_position[1].X > scrolling_items_texture[1].Width - screenWidth)
                spriteBatch.Draw(scrolling_items_texture[1], new Rectangle((int)(-1 * items_position[1].X) + scrolling_items_texture[1].Width, 0,
                    scaleWidth[1], scaleHeight[1]), Color.White);
         
            for (int i = 2; i < counts; i++) {
                if (canConsume[i]) {
                    rectangleCreator(i);
                    if (!consumed)
                        spriteBatch.Draw(scrolling_items_texture[i], items_rectangle[i], Color.White);
                }
                else  {
                    spriteBatch.Draw(scrolling_items_texture[i], new Rectangle((int)-items_position[i].X, (int)items_position[i].Y, scaleWidth[i], scaleHeight[i]), Color.White);
                }
            }
            spriteBatch.End();
        }

        private void rectangleCreator(int i)
        {
            if (canConsume[i])
                items_rectangle[i] = new Rectangle((int)-items_position[i].X, (int)items_position[i].Y, scaleWidth[i], scaleHeight[i]);
        }

        //Testing function. To be substituted with Physics.HasCollided
        private bool HasCollided(Rectangle source, Rectangle target)
        {
            return source.Intersects(target);
        }

        private void InteractedWithGameObjects(int i, Rectangle player_rectangle)
        {

            //TODO : Call Physics.cs - Method : HasCollided()
            //       here, check if playerRect & items_rectangle has collided
            //       if Yes, consumed = true. Else, consumed = false
            if (HasCollided(items_rectangle[i], player_rectangle))
            {
                consumed = true;
                //TODO : Add player HP
            }
        }

        /// <summary>
        /// ////////////////
        /// </summary>


        


    }
}
